home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / DarkenFilter.java < prev    next >
Text File  |  1998-08-21  |  3KB  |  118 lines

  1. package symantec.itools.awt.image;
  2.  
  3. import java.awt.image.RGBImageFilter;
  4. import java.awt.image.ColorModel;
  5. import java.awt.image.DirectColorModel;
  6. import java.lang.IllegalArgumentException;
  7. import java.util.ResourceBundle;
  8. import java.text.MessageFormat;
  9.  
  10. // Written by Levi Brown and Micheal Hopkins 1.1, July 8, 1997.
  11.  
  12. /**
  13.  * An Image filter to use for darkening an Image a specified percentage.
  14.  * @version 1.1, July 8, 1997
  15.  * @author  Symantec
  16.  */
  17. public class DarkenFilter extends RGBImageFilter
  18. {
  19.     /**
  20.      * Constructs a default DarkenFilter.
  21.      * By default the Image is darkened 50%.
  22.      * @see DarkenFilter#DarkenFilter(double)
  23.      * @see #setPercent
  24.      */
  25.     public DarkenFilter()
  26.     {
  27.         this(0.50);
  28.     }
  29.  
  30.     /**
  31.      * Constructs a DarkenFilter.
  32.      * @param percent the percent to darken the image when filtering.
  33.      * @see DarkenFilter#DarkenFilter()
  34.      * @see #setPercent
  35.      */
  36.     public DarkenFilter(double percent)
  37.     {
  38.         errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  39.  
  40.         canFilterIndexColorModel = true;
  41.         try
  42.         {
  43.             setPercent(percent);
  44.         }
  45.         catch (IllegalArgumentException exc)
  46.         {
  47.             Object[] args = { new Double(percent) };
  48.             System.err.println("DarkenFilter: " + errors.getString("InvalidPercent1"));
  49.             System.err.println("     " + errors.getString("InvalidPercent2"));
  50.             System.err.println("     " + errors.getString("InvalidPercent3"));
  51.             try { setPercent(0.50); } catch (IllegalArgumentException exc2) {}
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Sets the percentage to fade when filtering.
  57.      * @param percent the percentage to fade.
  58.      * @exception IllegalArgumentException
  59.      * if the specified percentage value is unacceptable
  60.      * @see #getPercent
  61.      */
  62.     public void setPercent(double percent) throws IllegalArgumentException
  63.     {
  64.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  65.  
  66.         this.percent = percent;
  67.     }
  68.  
  69.     /**
  70.      * Gets the percentage to fade when filtering.
  71.      * @return the percentage to fade.
  72.      * @see #setPercent
  73.      */
  74.     public double getPercent()
  75.     {
  76.         return percent;
  77.     }
  78.  
  79.     /**
  80.      * Filters an RGB value by the current fade percentage.
  81.      * @param x unused
  82.      * @param y unused
  83.      * @param rgb the rgb value to fade
  84.      * @return the faded rgb value
  85.      */
  86.     public int filterRGB( int x, int y, int rgb )
  87.     {
  88.         DirectColorModel cm = (DirectColorModel)ColorModel.getRGBdefault();
  89.  
  90.         int alpha = cm.getAlpha(rgb);
  91.         int red   = cm.getRed(rgb);
  92.         int green = cm.getGreen(rgb);
  93.         int blue  = cm.getBlue(rgb);
  94.  
  95.         red        = Math.max((int)(red    * (1 - percent)), 0);
  96.         green    = Math.max((int)(green    * (1 - percent)), 0);
  97.         blue    = Math.max((int)(blue    * (1 - percent)), 0);
  98.  
  99.         alpha    = alpha << 24;
  100.         red        = red   << 16;
  101.         green    = green << 8;
  102.  
  103.         return alpha | red | green | blue;
  104.     }
  105.  
  106.     /**
  107.      * The percentage to fade when filtering.
  108.      * @see #getPercent
  109.      * @see #setPercent
  110.      */
  111.     protected double percent;
  112.  
  113.     /**
  114.      * Error strings.
  115.      */
  116.     transient protected ResourceBundle errors;
  117. }
  118.